﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using Framework.Module.Adsvertising.Lib;
using Framework.Module.Adsvertising.Lib.Zones;
using Framework.Module.Adsvertising.Lib.DataAccess;
using Framework.Library.Components.Commands;

namespace Framework.Module.Adsvertising.Admin
{
    public partial class AdvertiseList : AuthenticatedControl
    {        
        private int PosSelected
        {
            get
            {
                if (Session["PosSelected"] != null)
                    return (int)Session["PosSelected"];
                else return 0;
            }
            set
            {
                Session["PosSelected"] = value;
            }
        }
        private void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lstPositions.DataSource = PositionDB.GetAll();
                lstPositions.DataTextField = "Pos_Name";
                lstPositions.DataValueField = "Pos_ID";
                lstPositions.DataBind();
                lstPositions.SelectedIndex = -1;
                MiscUtility.SetSelected(lstPositions.Items, PosSelected.ToString());

               
            }
            lblUpdateStatus.Text = string.Empty;
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            lstZones.Items.Clear();
            if (Constants.InZone)
            {
                ZoneController.LoadZones(lstZones.Items, AppEnv.PortalId());
                lstZones.Enabled = true;
            }
            else
            {
                lstZones.Enabled = false;
            }
            lstZones.Items.Insert(0, new ListItem((String)this.GetLocalResourceObject("HomePage"), "0"));
            lstZones.SelectedValue = "0";

        }
        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            if (lstPositions.SelectedItem == null) return;
            PosSelected = ConvertUtility.ToInt32(lstPositions.SelectedValue);
            string location = string.Empty;
            foreach (ListItem item in lstZones.Items)
            {
                if (item.Selected)
                {
                    location += item.Value + "|";
                }
            }
            DataTable source = AdvertiseDB.GetByPositionID(PosSelected, chkEnable.Checked, location);
            dtgAdvertises.DataSource = source;
            dtgAdvertises.DataBind();
        }

        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.AdsvertisingUpdate)));
        }

        protected void dtgAdvertises_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView curData = (DataRowView)e.Item.DataItem;
            string locations = "|" + curData["Advertise_Params"].ToString();
            string strLocation = string.Empty;
            foreach (ListItem item in lstZones.Items)
            {
                if (locations.IndexOf("|" + item.Value + "|") >= 0)
                    strLocation += item.Text + "<br>";
            }
            e.Item.Cells[2].Text = strLocation;

            Literal litUrl = (Literal)e.Item.FindControl("litUrl");

            switch (curData["Advertise_Type"].ToString())
            {
                case "flash":
                    litUrl.Text = MultimediaUtility.strInitFlash(curData["Advertise_Path"].ToString(), ConvertUtility.ToInt32(curData["Advertise_Width"]), ConvertUtility.ToInt32(curData["Advertise_Height"]));
                    break;
                case "media":
                    litUrl.Text = MultimediaUtility.strInitMultimedia(curData["Advertise_Path"].ToString(), Convert.ToInt32(curData["Advertise_Width"]), Convert.ToInt32(curData["Advertise_Height"]));
                    break;
                case "other":
                    litUrl.Text = curData["Advertise_Path"].ToString();
                    break;
                case "image":
                    if (curData["Advertise_Path"].ToString() != string.Empty)
                        litUrl.Text = "<img src='" + curData["Advertise_Path"].ToString() + "'></img>";
                    break;
            }
            litUrl.Text += "<br><a href='" + curData["Advertise_RedirectURL"].ToString() + "'>" + curData["Advertise_Name"].ToString() + "</a>";
            Button cmdDel = (Button)e.Item.FindControl("cmdDel");
            cmdDel.OnClientClick = "javascript:return confirm('" + (String)this.GetLocalResourceObject("DeleteConfirm") + "' )";
            DropDownList dropPriority = (DropDownList)e.Item.FindControl("dropPriority");
            MiscUtility.FillIndex(dropPriority, 0, 20, ConvertUtility.ToInt32(curData["Advertise_Priority"]));
        }

        protected void dtgAdvertises_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "edit")
            {
                Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.AdsvertisingUpdate)) + "&advertiseid=" + e.Item.Cells[0].Text);
            }
            if (e.CommandName == "del")
            {
                try
                {
                    AdvertiseDB.Delete(ConvertUtility.ToInt32(e.Item.Cells[0].Text));
                    lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateSuccess");
                    cmdSearch_Click(null, null);
                }
                catch
                {
                    lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateError");
                }
            }
            if (e.CommandName == "priority")
            {
                try
                {
                    foreach (DataGridItem item in dtgAdvertises.Items)
                    {
                        DropDownList dropPriority = (DropDownList)item.FindControl("dropPriority");
                        AdvertiseDB.SetPriority(ConvertUtility.ToInt32(item.Cells[0].Text), ConvertUtility.ToInt32(dropPriority.SelectedValue));
                    }
                    lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateSuccess");
                    cmdSearch_Click(null, null);
                }
                catch
                {
                    lblUpdateStatus.Text = (String)this.GetLocalResourceObject("UpdateError");
                }
            }
        }

    }
}
